package dhy.lyb.change.controller;

import dhy.lyb.change.model.ExtendModel;
import dhy.lyb.change.model.User;
import dhy.lyb.change.model.UserActivity;
import dhy.lyb.change.model.UserThree;
import dhy.lyb.change.service.UserService;
import dhy.lyb.change.service.UserThreeService;
import dhy.lyb.change.util.JsonUtil;
import javafx.scene.shape.VLineTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * 用户信息操作控制器
 * Created by Administrator on 2016/11/18.
 */
@Controller
@Scope("prototype")
public class UserInsController extends BaseController {
    @Autowired
    @Qualifier("userService")
    private UserService userService;

    @Autowired
    private UserThreeService userThreeService;

    /**
     * 用户信息注册接口
     *
     * @param user 请求信息json
     * @return extendsModel 发送信息json
     */
    @RequestMapping(value = "user/insertUser")
    @ResponseBody
    public ExtendModel insertUser(@RequestBody User user) {
        System.out.println(user);
        if (user == null) {
            return null;
        }
        int value = userService.insertRecord(user);
        //System.out.println("---------------------->" + value);
        if (value == 0) {
            return super.successResult(0);
        } else {

            SimpleDateFormat sdf =   new SimpleDateFormat("yyyy/MM/dd");
            String date = sdf.format(new Date());

            UserThree userThree = new UserThree();
            userThree.setId(user.getId());
            userThree.setWeight(user.getWeight());
            userThree.setHeight(user.getHeight());
            userThree.setDate(date);

            userThreeService.insertRecord(userThree);

            return super.insResult(value, user.getId());
        }
    }

    /**
     * 修改密码 忘记密码
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "user/updatePasswordByMobile")
    @ResponseBody
    public ExtendModel updatePasswordByMobile(@RequestBody User user) {
        System.out.println(user);
        if (user == null || "".equals(user.getMobile()) || "".equals(user.getPassword())) {
            return super.errorResult();
        }
        userService.updatePasswordByMobile(user);
        return super.successResult(0);
    }

    /**
     * 用户选择训练项目
     *
     * @param jsonCode
     * @return
     */
    @RequestMapping(value = "user/insertUserActivity")
    @ResponseBody
    public ExtendModel insertUserActivity(@RequestBody String jsonCode) {
        Map<String, Double> maps = JsonUtil.changeGsonToMaps(jsonCode);
        System.out.println(maps);
        double user_id = maps.get("user_id");
        double activity_id = maps.get("activity_id");
        if ("".equals(user_id) || user_id == 0 || "".equals(activity_id) || activity_id == 0) {
            return super.errorResult();
        }
        userService.insertUserActivity((int) user_id, (int) activity_id);
        return super.successResult(0);
    }

    /**
     * 用户删除训练项目
     *
     * @param jsonCode
     * @return
     */
    @RequestMapping(value = "user/deleteUserActivity")
    @ResponseBody
    public ExtendModel deleteUserActivity(@RequestBody String jsonCode) {
        Map<String, Double> maps = JsonUtil.changeGsonToMaps(jsonCode);
        System.out.println(maps);
        double user_id = maps.get("user_id");
        double activity_id = maps.get("activity_id");
        if ("".equals(user_id) || user_id == 0 || "".equals(activity_id) || activity_id == 0) {
            return super.errorResult();
        }
        userService.deleteUserActivity((int) user_id, (int) activity_id);
        return super.successResult(0);
    }

    /**
     * 用户更新训练信息
     *
     * @param userActivity
     * @return
     */
    @RequestMapping(value = "user/updateUserActivityUsers")
    @ResponseBody
    public ExtendModel updateUserActivityUsers(@RequestBody UserActivity userActivity) {
        System.out.println(userActivity);
        if (userActivity == null) {
            return super.errorResult();
        }
        userService.updateUserActivityUses(userActivity);
        return super.successResult(0);
    }

    @RequestMapping(value = "userThree/insertUserThree")
    @ResponseBody
    public ExtendModel insertUserThree(@RequestBody UserThree userThree){
        if(userThree == null){
            return super.errorResult();
        }
        userThreeService.insertRecord(userThree);
        return super.successResult(0);
    }
}
